<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>函数案例</title>
    <script>
        //函数求和
/*         function sum(x, y) {
            return x + y;
        }
        console.log(sum(1, 100)); */

        //函数累加
/*         function sum(from, to) {
            var sum = 0;
            for (var i = from; i <= to; i++) {
                sum += i;
            }
            return sum;
        }
        console.log(sum(1, 100)); */
        
        //函数比大小
/*         function max1(x, y) {
            if (x > y)
                return x;
            else
                return y;
        }
        function max2(x, y) {
            return x > y ? x : y;
        } 
        console.log(max1(1, 11));
        console.log(max2(1, 11)); */

        //函数数组求最大值
/*         function arrmax(arr) {
            var max = arr[0];
            for (var i = 0; i < arr.length; i++) {
            if (arr[i] > max)
                max = arr[i];
            }
            return max;
        }
        var arr = [8, 2, 3, 6, 5];
        console.log(arrmax(arr)); */

        //函数未知实参个数时求最大值
/*         function max() {
            var max = arguments[0];
            for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] > max)
                max = arguments[i];
            }
            return max;
        }
        console.log(max(1, 8, 5, 7, 20, 9)); */

        //函数翻转数组
/*         function reverse(arr) {
            var newArr = [];
            for (var i = arr.length - 1; i >= 0; i--) {
                newArr[newArr.length] =  arr[i];
            }
            return newArr;
        }
        console.log(reverse([1, 2, 3, 4, 5])); */
        
        //函数冒泡排序
/*         function bubbleSort(arr) {
            var temp;
            for (var i = 0; i < arr.length; i++) {
                for (var j = 0; j < arr.length - i; j++) {
                    if (arr[j + 1] < arr[j]) {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            return arr;
        }
        console.log(bubbleSort([5, 4, 3, 2, 1])); */

        //函数判断闰年,2月份天数
/*         function leapYear1(year) {
            return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? '是闰年' : '不是闰年';
        }

        function leapYear2(year) {
            return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? true : false;
        }

        function febDays() {
            if(leapYear2(year))
                alert('是闰年29天');
            else
                alert('不是闰年28天');
        }

        var year = prompt('请输入年份:');
        //alert(leapYear1(year));
        febDays(); */

        //
    </script>
</head>
<body>
    
</body>
</html>